package l1j.opqlo.Server.L1World;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.server.server.model.L1CastleLocation;
import l1j.server.server.model.L1Clan;


/**
 * 世界血盟暫存區<BR>
 *
 * @author dexc
 *
 */
public class WorldClan {

	private static final Logger _log = Logger.getLogger(WorldClan.class.getName());

	private static WorldClan _instance;

	public static WorldClan get() {
		if (_instance == null) {
			_instance = new WorldClan();
		}
		return _instance;
	}

	private final ConcurrentHashMap<String, L1Clan> _isClan;

	private Collection<L1Clan> _allClanValues;

	private WorldClan() {
		this._isClan = new ConcurrentHashMap<String, L1Clan>();
	}

	/**
	 * 有城堡的血盟清單
	 * @return
	 */
	public HashMap<Integer, String> castleClanMap() {
		// <城堡編號, 血盟名稱>
		final HashMap<Integer, String> isClan = new HashMap<Integer, String>();

		for (L1Clan clan : this.getAllClans()) {
			if (clan.getCastleId() != 0) {
				isClan.put(clan.getCastleId(), clan.getClanName());
			}
		}
		return isClan;
	}

	/**
	 * 全部血盟
	 * @return
	 */
	public Collection<L1Clan> getAllClans() {
		try {
			final Collection<L1Clan> vs = this._allClanValues;
			return vs != null ? vs : (this._allClanValues = Collections.unmodifiableCollection(this._isClan.values()));
			//return Collections.unmodifiableCollection(_isCrown.values());

		} catch (final Exception e) {
			_log.log(Level.SEVERE ,e.getLocalizedMessage(), e);
		}
		return null;
	}

	/**
	 * 傳回血盟數據
	 * @param clan_name
	 * @return
	 */
	public L1Clan getClan(final String clan_name) {
		return this._isClan.get(clan_name);
	}

	/**
	 * 血盟清單
	 * @return
	 */
	public ConcurrentHashMap<String, L1Clan> map() {
		return this._isClan;
	}

	/**
	 * 加入血盟清單
	 * @param key
	 * @param value
	 */
	public void put(final String key, final L1Clan value) {
		try {
			this._isClan.put(key, value);

		} catch (final Exception e) {
			_log.log(Level.SEVERE,e.getLocalizedMessage(), e);
		}
	}

	/**
	 * 移出血盟清單
	 * @param key
	 */
	public void remove(final String key) {
		try {
			this._isClan.remove(key);

		} catch (final Exception e) {
			_log.log(Level.SEVERE ,e.getLocalizedMessage(), e);
		}
	}

	/**
	 * 移出血盟清單
	 * @param clan
	 */
	public void removeClan(final L1Clan clan) {
		final L1Clan temp = this.getClan(clan.getClanName());
		if (temp != null) {
			this._isClan.remove(clan.getClanName());
		}
	}

	/**
	 * 加入血盟清單
	 * @param clan
	 */
	public void storeClan(final L1Clan clan) {
		final L1Clan temp = this.getClan(clan.getClanName());
		if (temp == null) {
			this._isClan.put(clan.getClanName(), clan);
			final int castle_id = clan.getCastleId();
			if (castle_id != 0) {
				if (L1CastleLocation.mapCastle().get(new Integer(castle_id)) == null) {
					L1CastleLocation.putCastle(new Integer(castle_id), clan);
				}
			}
		}
	}
}
